package moco.blatt9.soapwebservices;

import java.io.IOException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class CelsiusFahrenheit extends Activity{
	
	private Button btnClear, btnToC, btnToF;
	private EditText inC, inF;
	Context c = this;
	
	private static final String SOAP_ACTION_F2C = "http://tempuri.org/FahrenheitToCelsius";
    private static final String SOAP_ACTION_C2F = "http://tempuri.org/CelsiusToFahrenheit";
    private static final String NAMESPACE = "http://tempuri.org/";
    private static final String WSDL_METHOD_NAME_F2C = "FahrenheitToCelsius";
    private static final String WSDL_METHOD_NAME_C2F = "CelsiusToFahrenheit";
    private static final String WSDL_URL = "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";
    

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.celsius_fahrenheit);
		
		inC = (EditText) findViewById(R.id.txtc);
		inF = (EditText) findViewById(R.id.txtf);
		
		_init_btnClear();		// Clear Button
		_init_btnToC(this);		// F -> C
		_init_btnToF(this);		// C -> F
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(item.getItemId() == R.id.call_addition_activity) {
			Intent addition = new Intent();
			addition.setClassName(this, Addition.class.getName());
			startActivity(addition);
		} else if(item.getItemId() == R.id.call_local_activity){
			Intent loc = new Intent();
			loc.setClassName(this, MainActivity.class.getName());
			startActivity(loc);
		}
		return super.onOptionsItemSelected(item);
	}
	
	
	/* **************************************************************************
	 * 							PRIVATE METHODES								*
	 * ************************************************************************** */
	
	private void _init_btnClear(){
		btnClear = (Button) findViewById(R.id.btnClear);
		btnClear.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				inC.setText("0");
				inF.setText("0");
			}
		});
	}
	
	private void _init_btnToC(Context context){
		btnToC = (Button) findViewById(R.id.btn2C);
		btnToC.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				_toC();
			}
		});
	}
	
	private void _toC(){
		new AsyncTask<String, String, String>() {
			
			@Override
			protected String doInBackground(String... params) {
				String ret = "#";
				SoapObject request = new SoapObject(NAMESPACE, WSDL_METHOD_NAME_F2C);
				request.addProperty("Fahrenheit", params[0]);
				
				SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
				envelope.setOutputSoapObject(request);
				envelope.dotNet = true;
				
				try {
					
					HttpTransportSE httptransport = new HttpTransportSE(WSDL_URL);
//					httptransport.debug = true;
					httptransport.call(SOAP_ACTION_F2C, envelope);
					
//					System.out.println("DEBUG: " + httptransport.requestDump);
					
					if(envelope.bodyIn instanceof SoapFault){
						SoapFault sf = (SoapFault) envelope.bodyIn;
						ret = sf.faultstring + ". " + sf.getMessage() + ". " + sf.getCause();
						Log.e("DEBUG",ret);
					} else {
						SoapObject response = (SoapObject) envelope.bodyIn;
						ret = response.getProperty(0).toString();
					}
					
				} catch (IOException e) {
					System.out.println("Fehler1" + e.getMessage() + ". " + e.getCause());
				} catch (XmlPullParserException e) {
					System.out.println("Fehler2" + e.getMessage() + ". " + e.getCause());
				}
				return ret;
			}
			
			protected void onPostExecute(String result) {
				inC.setText(result);
				super.onPostExecute(result);
			};
			
		}.execute(inF.getText().toString());
	}
	
	
	private void _init_btnToF(final Context context){
		btnToF = (Button) findViewById(R.id.btn2F);
		btnToF.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				_toF();
			}
		});
	}
	
	private void _toF(){
		new AsyncTask<String, String, String>() {
			
			@Override
			protected String doInBackground(String... params) {
				Log.d("DEBUG", params.length + ", " + params[0]);
				String ret = "#";
				SoapObject request = new SoapObject(NAMESPACE, WSDL_METHOD_NAME_C2F);
				request.addProperty("Celsius", params[0]);
				
				SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
				envelope.setOutputSoapObject(request);
				envelope.dotNet = true;
				
				try {
					HttpTransportSE httptransport = new HttpTransportSE(WSDL_URL);
					httptransport.call(SOAP_ACTION_C2F, envelope);
					
					if(envelope.bodyIn instanceof SoapFault){
						SoapFault sf = (SoapFault) envelope.bodyIn;
						ret = sf.faultstring + ". " + sf.getMessage() + ". " + sf.getCause();
					} else {
						SoapObject response = (SoapObject) envelope.bodyIn;
						ret =  response.getProperty(0).toString();
						Log.d("DEBUG", "Hier?" + response.toString());
					}
					
				} catch (IOException e) {
					System.out.println("Fehler1" + e.getMessage() + ". " + e.getCause());
				} catch (XmlPullParserException e) {
					System.out.println("Fehler2" + e.getMessage() + ". " + e.getCause());
				}
				return ret;
			}
			
			protected void onPostExecute(String result) {
				inF.setText(result);
				super.onPostExecute(result);
			};
			
		}.execute(inC.getText().toString());
	}
	
}